cap log close

global path = "***"

local name= "penalty_exempt_controls"  
local date = "062220"
global logname = "$path/`name'_`date'.log"
set matsize 11000

clear
set more off
cd "$path"
log using "$logname", replace


use "****/f8965_2014",clear
duplicates drop tin,force
merge 1:m tin using "$path/Data/penalty_sample.dta",keep(2 3)
gen exempt2014 = _merge==3
drop _merge

rename fpl_frac magi_fpl2015
replace age_2017=0 if age_2017<0


*create control variables
replace age_2017=0 if age_2017<0

gen age_45_64 = age_2017 < 65 & age_2017 >= 45

gen log_baorhigher = log(baorhigher)

gen log_medhhinc = log(medhhinc)

gen log_pre_spanish = log(pre_spanish)

gen cov2016_0 = (covered2016 - any_dec ==0)

gen cov2016_11 = (covered2016 - any_dec == 11)

gen negative_income = (magi_fpl2015 < 0)

bysort state_num: egen state_mort2016_45 = mean(dead16) if age_45_64 == 1
bysort state_num: egen state_mort2016 = mean(dead16) 


local replace_missing_vals log_baorhigher log_medhhinc log_pre_spanish magi_fpl2015
					 
foreach var of varlist `replace_missing_vals'{
gen is_missing_`var' = (`var' == .)
gen `var'_corrected = 0 if `var' == . 
replace `var'_corrected = `var' if `var' != . 
}

egen fpl15cat_corrected =cut(magi_fpl2015_corrected),at(-10000000,1,1.38,2.5,4,8,10000000000)
tabulate fpl15cat_corrected, generate(income_corrected)

*Table:	Effect of Treatment on Coverage (overall, all16, notall16)
estimates clear
eststo clear

*Panel A: All ages

local controls i.age_2017 male married cov2016_0 state_mort2016 income_corrected2-income_corrected6  ///
			   negative_income penalize2014 log_baorhigher_corrected log_medhhinc_corrected log_pre_spanish_corrected ///
			   is_missing_log_baorhigher is_missing_log_medhhinc is_missing_log_pre_spanish is_missing_magi_fpl2015 ///
			   exempt2014


				
*Column 1: any_coverage1718 (overall)
reg any_covered1718 treatment `controls', cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/cov_controls_`date'.ster", replace

*Column 2: coverage1718 (overall)
reg covered1718 treatment `controls', cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)
estimates save "$path/cov_controls_`date'.ster", append

*Column 3:  any_coverage1718 (all16)
reg any_covered1718 treatment `controls' if cov2016_11 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/cov_controls_`date'.ster", append

*Column 4: coverage1718 (all16)
reg covered1718 treatment `controls' if cov2016_11 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)
estimates save "$path/cov_controls_`date'.ster", append

*Column 5: any_coverage1718 (notall16)
reg any_covered1718 treatment `controls' if notall16 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/cov_controls_`date'.ster", append

*Column 6:  coverage1718 (notall16)
reg covered1718 treatment `controls' if  notall16 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)
estimates save "$path/cov_controls_`date'.ster", append

*Panel B: Ages 45-64
local controls i.age_2017 male married cov2016_0 state_mort2016_45 income_corrected2-income_corrected6  ///
			   negative_income penalize2014 log_baorhigher_corrected log_medhhinc_corrected log_pre_spanish_corrected ///
			   is_missing_log_baorhigher is_missing_log_medhhinc is_missing_log_pre_spanish is_missing_magi_fpl2015 ///
			   exempt2014

				
*Column 1: any_coverage1718 (overall)
reg any_covered1718 treatment `controls' if age_45_64 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/cov_controls_`date'.ster", append

*Column 2: coverage1718 (overall)
reg covered1718 treatment `controls' if age_45_64 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)
estimates save "$path/cov_controls_`date'.ster", append

*Column 3:  any_coverage1718 (all16)
reg any_covered1718 treatment `controls' if cov2016_11 == 1 & age_45_64 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/cov_controls_`date'.ster", append

*Column 4: coverage1718 (all16)
reg covered1718 treatment `controls' if cov2016_11 == 1 & age_45_64 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)
estimates save "$path/cov_controls_`date'.ster", append

*Column 5: any_coverage1718 (notall16)
reg any_covered1718 treatment `controls' if notall16 == 1 & age_45_64 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/cov_controls_`date'.ster", append

*Column 6:  coverage1718 (notall16)
reg covered1718 treatment `controls' if  notall16 == 1 & age_45_64 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)
estimates save "$path/cov_controls_`date'.ster", append


*Table: Non-Linear Models
local controls i.age_2017 male married cov2016_0 state_mort2016_45 income_corrected2-income_corrected6  ///
			   negative_income penalize2014 log_baorhigher_corrected log_medhhinc_corrected log_pre_spanish_corrected ///
			   is_missing_log_baorhigher is_missing_log_medhhinc is_missing_log_pre_spanish is_missing_magi_fpl2015 exempt2014


*Col 1: Reduced Form with Controls
reg dead1718 treatment `controls' if age_45_64==1 & notall16==1, vce(cl tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/mort_nonlinear_`date'.ster", replace

*col 4: Logit with Controls
logit dead1718 i.treatment `controls' if age_45_64==1 & notall16==1, cl(tin)
eststo
estadd scalar nobs e(N)
cap mat drop b
mat b = r(table)
estadd scalar pval = b[4,2]
summ `e(depvar)' if e(sample)&treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/mort_nonlinear_`date'.ster", append
margins treatment, atmeans post  
eststo
estimates save "$path/mort_nonlinear_`date'.ster", append

*set up variables for survival analysis
sort yod mod
cap drop period
egen period = group(yod mod) if dead1718 == 1
replace period = 24 if dead1718 == 0
*col 6: cox model with controls 
stcox treatment `controls' if age_45_64==1 & notall16==1 & dead16 == 0, cl(tin) 
eststo
estadd scalar nobs = e(N_sub)
cap mat drop b
mat b = r(table)
estadd scalar pval = b[4,1]
estadd scalar ymean=r(mean)
estimates save "$path/mort_nonlinear_`date'.ster", append

log close

